How to use loc[] to select all columns, except one given column In Python Pandas
This w3wiki Dataframe is just a two dimension array with numerical index. Therefore, to except only one column we could use the columns methods to get all columns and use a not operator to exclude the columns which are not needed. This method works only when the Dataframe is not multi indexed (did not have more than one index).
Example 1: Using Dataframe.loc[ ] with ‘!=’
Python3
df = data.loc[ : , data.columns ! = 'student_gender' ] # show the dataframe df |
Output:
Example 2: Using Dataframe.loc[ ] with isin
Python3
df = data.loc[:, ~data.columns.isin([ 'student_gender' ])] # showing the dataframe df |
Output:
Select all columns, except one given column in a Pandas DataFrame
DataFrame Data structure are the heart of Pandas library. DataFrames are basically two dimension Series object. They have rows and columns with rows representing the index and columns representing the content. Now, let’s see how to Select all columns, except one given column in Pandas DataFrame in Python.
Contact Us